package univ.bme.accounting.domain;

import java.util.List;
import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor

@Entity
@Access(AccessType.FIELD)
@Table(name="categories")
public class Category {
	
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private int id;
	
	@OneToMany(mappedBy = "category", cascade = CascadeType.REMOVE)
	private List<Transaction> transactions;	
	
	
	@ManyToOne(targetEntity = User.class, optional = false )
	@Fetch( FetchMode.SELECT )
	@JoinColumn(name = "user_id")
	private User user;
	
	@Column(length=30,nullable=false)
	private String name;
	
	 public boolean equals(Object object) {
	        // Basic checks.
	        if (object == this) return true;
	        if (object == null || getClass() != object.getClass()) return false;

	        // Property checks.
	        Category other = (Category) object;
	        if (name == null ? other.name != null : !name.equals(other.name)) return false;
	        if (id != other.id) return false;

	        // All passed.
	        return true;
	}
}
